package com.lineage.server.clientpackets;

import com.eric.gui.J_Main;
import com.lineage.config.Config;
import com.lineage.config.ConfigAlt;
import com.lineage.config.ConfigRecord;
import com.lineage.echo.ClientExecutor;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.model.Instance.L1DeInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Object;
import com.lineage.server.serverpackets.S_ChatWhisperFrom;
import com.lineage.server.serverpackets.S_ChatWhisperTo;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldNpc;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class C_ChatWhisper extends ClientBasePacket {
  private static final Log _log = LogFactory.getLog(C_ChatWhisper.class);
  
  public void start(byte[] decrypt, ClientExecutor client) {
    try {
      read(decrypt);
      L1PcInstance whisperFrom = client.getActiveChar();
      if (whisperFrom.hasSkillEffect(4002)) {
        whisperFrom.sendPackets((ServerBasePacket)new S_ServerMessage(242));
      } else if (whisperFrom.getLevel() < ConfigAlt.WHISPER_CHAT_LEVEL && !whisperFrom.isGm()) {
        whisperFrom.sendPackets((ServerBasePacket)new S_ServerMessage(404, String.valueOf(ConfigAlt.WHISPER_CHAT_LEVEL)));
      } else {
        String targetName = readS();
        String text = readS();
        if (text.length() > 52) {
          _log.warn("人物:" + whisperFrom.getName() + "对话长度超过限制:" + client.getIp().toString());
          client.set_error(client.get_error() + 1);
          return;
        } 
        L1PcInstance whisperTo = World.get().getPlayer(targetName);
        L1DeInstance de = null;
        if (whisperTo == null)
          de = getDe(targetName); 
        if (de != null) {
          whisperFrom.sendPackets((ServerBasePacket)new S_ChatWhisperTo(de, text));
          return;
        } 
        if (whisperTo == null) {
          whisperFrom.sendPackets((ServerBasePacket)new S_ServerMessage(73, targetName));
          return;
        } 
        if (whisperTo.isBadInEnemyList(whisperFrom.getName()))
          return; 
        if (whisperTo.equals(whisperFrom))
          return; 
        if (whisperTo.getExcludingList().contains(whisperFrom.getName())) {
          whisperFrom.sendPackets((ServerBasePacket)new S_ServerMessage(117, whisperTo.getName()));
          return;
        } 
        if (!whisperTo.isCanWhisper()) {
          whisperFrom.sendPackets((ServerBasePacket)new S_ServerMessage(205, whisperTo.getName()));
          return;
        } 
        if (whisperTo.hasSkillEffect(9990)) {
          whisperTo.sendPackets((ServerBasePacket)new S_SystemMessage("目前受到监禁状态中无法正常说话。"));
          return;
        } 
        if (ConfigAlt.GM_OVERHEARD) {
          Iterator<L1PcInstance> iterator = World.get().getAllPlayers().iterator();
          while (iterator.hasNext()) {
            L1Object visible = (L1Object)iterator.next();
            if (visible instanceof L1PcInstance) {
              L1PcInstance GM = (L1PcInstance)visible;
              if (GM.isGm() && whisperFrom.getId() != GM.getId() && GM.getChatListenWhisper().booleanValue())
                GM.sendPackets((ServerBasePacket)new S_SystemMessage(
                      "\\fV【密语】" + whisperFrom.getName() + " -> " + targetName + ":" + text)); 
            } 
          } 
        } 
        whisperFrom.sendPackets((ServerBasePacket)new S_ChatWhisperTo(whisperTo, text));
        whisperTo.sendPackets((ServerBasePacket)new S_ChatWhisperFrom(whisperFrom, text));
        if (ConfigRecord.LOGGING_CHAT_WHISPER)
          RecordTable.get().recordeTalk("密语", whisperFrom.getName(), whisperFrom.getClanname(), 
              whisperTo.getName(), text); 
        if (Config.GUI) {
          J_Main.getInstance().addPrivateChat(whisperFrom.getName(), whisperTo.getName(), text);
          return;
        } 
        return;
      } 
      return;
    } catch (Exception exception) {
    
    } finally {
      over();
    } 
  }
  
  public static L1DeInstance getDe(String s) {
    Collection<?> allNpc = WorldNpc.get().all();
    if (allNpc.isEmpty())
      return null; 
    Iterator<?> iter = allNpc.iterator();
    while (iter.hasNext()) {
      L1NpcInstance npc = (L1NpcInstance)iter.next();
      if (npc instanceof L1DeInstance) {
        L1DeInstance de = (L1DeInstance)npc;
        if (de.getNameId().equalsIgnoreCase(s))
          return de; 
      } 
    } 
    return null;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
